package cn.edu.cqytxy;

import cn.edu.cqytxy.entity.Cat;

import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * io测试类
 *
 * @author yinjw
 */
public class IOTest {

    public static void main(String[] args) throws Exception {

        // 1 创建对象
        Cat cat = new Cat();
        cat.setName("tom");
        cat.setAge(3);

        // 2 对象序列化
        OutputStream os = new FileOutputStream("D:\\Temp\\cat.txt");
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
        objectOutputStream.writeObject(cat);

        // 1 对象反序列化
        InputStream is = new FileInputStream("D:\\Temp\\cat.txt");
        ObjectInputStream objectInputStream = new ObjectInputStream(is);
        Object o = objectInputStream.readObject();

        // 对象强转为序列化前的类型
        Cat catFile = (Cat) o;
        System.out.println(catFile.getName());
        System.out.println(catFile.getAge());
    }

    private static void testIODemo9() throws IOException {
        URL url = new URL("https://www.baidu.com/img/PCfb_5bf082d29588c07f842ccde3f97243ea.png");
        InputStream inputStream = url.openStream();
        BufferedInputStream bis = new BufferedInputStream(inputStream, 8192);

        OutputStream os = new FileOutputStream("D:\\Temp\\baidu.png");
        BufferedOutputStream bos = new BufferedOutputStream(os);

        int i = 0;
        while ((i = bis.read()) != -1) {
            bos.write(i);
        }
        bos.close();
    }

    private static void testIODemo8() throws IOException {
        // 字节字符转换流
        InputStream inputStream = new FileInputStream("D:\\Temp\\yq.txt");
        InputStreamReader isr = new InputStreamReader(inputStream, "GBK");
        BufferedReader br = new BufferedReader(isr);

        OutputStream os = new  FileOutputStream("D:\\Temp\\yq20.txt");
        OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
        BufferedWriter bw = new BufferedWriter(osw);

        String str = null;
        while ((str = br.readLine()) != null) {
            System.out.println(str);
            bw.write(str);
            bw.newLine();
        }
        bw.close();
    }

    private static void testIODemo7() throws IOException {
        // BufferedInputStream
        InputStream is = new FileInputStream("D:\\Temp\\yq.txt");
        BufferedInputStream bis = new BufferedInputStream(is);
        int i = bis.read();
        System.out.println((char)i);

        // BufferedOutputStream
        OutputStream os = new FileOutputStream("D:\\Temp\\yq20.txt");
        BufferedOutputStream bos = new BufferedOutputStream(os);
        bos.write(97);

        // 刷新缓冲区或调用bos.close();关闭后自动刷新缓冲区
        bos.flush();
    }

    private static void testIODemo6() throws IOException {
        // 创建字符输入流和字符输出流
        Reader reader = new FileReader("D:\\Temp\\yq5.txt");
        Writer writer = new FileWriter("D:\\Temp\\yq6.txt");
        int index;
        char[] chars = new char[3];
        while ((index = reader.read(chars)) != -1) {
            // 循环读取和写入
            System.out.println(index);
            writer.write(chars, 0, index);
        }

        // 关闭流
        reader.close();
        writer.close();
    }

    private static void testIODemo5() throws IOException {
        InputStream is = new FileInputStream("D:\\Temp\\yq.txt");


        // 初始化一个数组用来传给read方法
        byte[] bytes = new byte[2];
        // 第一次遍历数组，查看初始值
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }

        System.out.println("================");

        int index = is.read(bytes);
        // 第二次遍历数组，查最终值
        for (int i = 0; i < bytes.length; i++) {
            System.out.println((char) bytes[i]);
        }
        // 查看read的最终值
        System.out.println("index = " + index);
    }

    private static void testIODemo4() {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {

            inputStream = new FileInputStream("E:\\cqyt\\三期\\打包\\20250226java变量标识符运算等\\20250226java变量标识符运算等.mp4");
            outputStream = new FileOutputStream("E:\\cqyt\\三期\\打包\\20250226java变量标识符运算等\\20250226java变量标识符运算等2.mp4");

            // 获取文件数据并写入到另外一个文件
            int index;
            byte[] bytes = new byte[1024 * 1024];
            while ((index = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0 ,index);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void testIODemo3() throws IOException {
        OutputStream outputStream = new FileOutputStream("D:\\Temp\\yq2.txt");
        outputStream.write(97);
    }

    private static void testIODemo2() {
        InputStream inputStream = null;
        try {
            // 1创建字节输入流
            inputStream = new FileInputStream("D:\\Temp\\yq.txt");

            // 2获取文件数据
            int read;
            while ((read = inputStream.read()) != -1) {
                // read不等于-1时代表还有数据，等于-1时结束循环
                System.out.print((char) read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void testIODemo1() throws IOException{
        // 1拿到文件输入流
        InputStream inputStream = new FileInputStream("D:\\Temp\\yq.txt");

        // 2获取文件数据
        int read1 = inputStream.read();
        System.out.println((char)read1);
        int read2 = inputStream.read();
        System.out.println((char)read2);
        int read3 = inputStream.read();
        System.out.println((char)read3);
        int read4 = inputStream.read();
        System.out.println(read4);
    }
}
